<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Group
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: AdminManageController.php 9086 2011-07-21 21:25:21Z john $
 * @author     Jung
 */

/**
 * @category   Application_Extensions
 * @package    Group
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Group_AdminManageController extends Core_Controller_Action_Admin
{

  public function indexAction()
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('group_admin_main', array(), 'group_admin_main_manage');

        // Filter form
        $title = $this->_getParam('title');
        $owner = $this->_getParam('owner');
        $order = $this->_getParam('order');

        $this->view->form = $form = new Group_Form_Admin_Search();
        if(!empty($title)){
            $form->populate(array(
                'title' => $title,
            ));
        }
        if(!empty($owner)){
            $form->populate(array(
                'owner' => $owner,
            ));
        }
        if(!empty($order)){
            $form->populate(array(
                'order' => $order,
            ));
        }
        
    if ($this->getRequest()->isPost())
    {
      $values = $this->getRequest()->getPost();
      foreach ($values as $key=>$value) {
        if ($key == 'delete_' . $value)
        {
          $group = Engine_Api::_()->getItem('group', $value);
          $group->delete();
        }
      }
        $fvalues = $form->getValues();
        if($fvalues['form_search'] == 'form_search'){
            $this->view->title = $fvalues['title'];
            $this->view->owner = $fvalues['owner'];
            $this->view->order = $fvalues['order'];
        }
        //echo $select; die;
        //$select = $table->select();
    }
    $table = Engine_Api::_()->getDbTable('groups','group');
    $select = $table->select()
                    ->setIntegrityCheck ( false )
                  ->from(array('group'=>'engine4_group_groups'))
                  ->join(array('user'=>'engine4_users'),'group.user_id = user.user_id','displayname')  
                  ; 
    if(!empty($title)){
        $select->where('title like ?',$title);
    }
    if(!empty($owner)){
        $select->where('displayname like ?',$owner);
    }
    if(!empty($order)){
        if($order == '1')
            $select->order('modified_date DESC');
        else   
            $select->order('view_count DESC');
    }
//    echo $select; die;
    $page = $this->_getParam('page', 1);
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);   
    $this->view->paginator->setItemCountPerPage(25);
    $this->view->paginator->setCurrentPageNumber($page);
  }

  public function deleteAction()
  {
    // In smoothbox
    $this->_helper->layout->setLayout('admin-simple');
    $id = $this->_getParam('id');
    $this->view->group_id=$id;
    // Check post
    if( $this->getRequest()->isPost())
    {
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();

      try
      {
        $group = Engine_Api::_()->getItem('group', $id);
        $group->delete();
        $db->commit();
      }

      catch( Exception $e )
      {
        $db->rollBack();
        throw $e;
      }

      $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => 10,
          'parentRefresh'=> 10,
          'messages' => array('')
      ));
    }
    // Output
    $this->renderScript('admin-manage/delete.tpl');
  }
}